package com.jt.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jt.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RefreshScope
public class ProviderController {
    //  private static final  Logger log = LoggerFactory.getLogger(ProviderController.class);
    @Value("${server.port:8080}")
    private String server;

    @GetMapping("/provider/echo/{msg}")
    public String doRestEcho1(@PathVariable("msg") String msg) throws InterruptedException {
        Thread.sleep(5000);
        log.debug("doRestEcho1,start:{}", System.currentTimeMillis());
        return server + "say hello" + msg;
    }

    @GetMapping("/provider/log/doLog01")
    public String doLog01() {//trace<debug<info<warn<error
        System.out.println("==doLog01==");
        log.trace("===trace===");
        log.debug("===debug===");
        log.info("===info====");
        log.warn("===warn===");
        log.error("===error===");
        return "log config test";
    }

    @Value("${logging.level.com.com.jt:error}")
    private String logLevel;

    @GetMapping("/provider/log/doLog02")
    public String doLog02() {
        log.info("log level is  {}", logLevel);
        return "log level  is " + logLevel;
    }

    @GetMapping("/provider/sentinel01")
    public String doSentinel01() {
        return "sentinel 01 test  ...";
    }
    @GetMapping("/provider/sentinel02")
    public String doSentinel02(){
        return "sentinel 02 test  ...";
    }
    @Autowired
    private ResourceService resourceService;
    @GetMapping("/provider/sentinel03")
    public String doSentinel03() throws InterruptedException {
        resourceService.doGetResource();
        return "sentinel 03 test";
    }
    @GetMapping("/provider/sentinel/findById")
    @SentinelResource("resource")
    public String doFindById(@RequestParam("id") Integer id){
        return "resource id is "+id;
    }


}
